import { _decorator } from 'cc';
import { BaseEvolutionTree } from './BaseEvolutionTree';
import { GameEvents } from '../constants/GameConstants';
import { EventManager } from '../core/EventManager';

const { ccclass, property } = _decorator;

/**
 * 飞行生物进化树
 * 
 * 功能：
 * 1. 定义飞行生物进化路线
 * 2. 管理飞行能力发展
 * 3. 处理空中特殊能力
 * 4. 提供飞行适应性
 * 
 * 特点：
 * 1. 翼展发展系统
 * 2. 空气动力学优化
 * 3. 高空适应能力
 * 4. 空中机动性
 */
@ccclass('AerialEvolutionTree')
export class AerialEvolutionTree extends BaseEvolutionTree {
    onLoad() {
        this.currentStage = 'basic_flyer';
        this.initEvolutionTree();
    }

    private initEvolutionTree() {
        // 基础飞行者
        this.addEvolutionNode('basic_flyer', {
            name: '基础飞行者',
            description: '初步掌握飞行能力',
            requirements: [],
            effects: {
                wingSpan: 5,
                flightSpeed: 8,
                glideEfficiency: 10
            },
            skills: ['basic_flight', 'glide']
        });

        // 高空翱翔者
        this.addEvolutionNode('high_soarer', {
            name: '高空翱翔者',
            description: '适应高空飞行',
            requirements: ['basic_flyer'],
            effects: {
                altitudeLimit: 20,
                oxygenEfficiency: 15,
                thermalSoaring: 12
            },
            skills: ['thermal_ride', 'altitude_mastery']
        });

        // 敏捷猎手
        this.addEvolutionNode('agile_hunter', {
            name: '空中猎手',
            description: '精通空中捕食技巧',
            requirements: ['basic_flyer'],
            effects: {
                agility: 20,
                divingSpeed: 25,
                huntingPrecision: 15
            },
            skills: ['dive_attack', 'aerial_maneuver']
        });

        // 风暴掠食者
        this.addEvolutionNode('storm_predator', {
            name: '风暴掠食者',
            description: '能在恶劣天气中飞行的掠食者',
            requirements: ['agile_hunter'],
            effects: {
                stormResistance: 20,
                windControl: 15,
                weatherSense: 10
            },
            skills: ['storm_flight', 'wind_mastery']
        });

        // 迁徙专家
        this.addEvolutionNode('migration_master', {
            name: '迁徙专家',
            description: '擅长长距离飞行',
            requirements: ['high_soarer'],
            effects: {
                endurance: 25,
                energyEfficiency: 20,
                navigationSense: 15
            },
            skills: ['efficient_flight', 'navigation']
        });

        // 空中杂技大师
        this.addEvolutionNode('aerial_acrobat', {
            name: '空中杂技大师',
            description: '掌握高难度飞行技巧',
            requirements: ['agile_hunter'],
            effects: {
                maneuverability: 25,
                turnRate: 20,
                balanceControl: 15
            },
            skills: ['barrel_roll', 'loop_maneuver']
        });
    }

    public evolve(targetStage: string): boolean {
        if (!this.canEvolve(targetStage)) return false;

        const node = this.evolutionNodes.get(targetStage);
        if (!node) return false;

        // 解锁进化节点
        node.unlock();
        this.currentStage = targetStage;

        // 应用进化效果
        this.applyEvolutionEffects(node);
        this.applyAerialEffects(node);

        // 发送进化完成事件
        EventManager.instance.emit(GameEvents.EVOLUTION_COMPLETE, {
            stage: targetStage,
            effects: node.effects,
            type: 'aerial'
        });

        return true;
    }

    private applyAerialEffects(node: EvolutionNode) {
        const flightSystem = this.getComponent('FlightSystem');
        if (!flightSystem) return;

        // 更新飞行能力
        if (node.effects.wingSpan) {
            flightSystem.updateWingSpan(node.effects.wingSpan);
        }
        if (node.effects.flightSpeed) {
            flightSystem.updateFlightSpeed(node.effects.flightSpeed);
        }
        if (node.effects.altitudeLimit) {
            flightSystem.setAltitudeLimit(node.effects.altitudeLimit);
        }

        // 应用特殊飞行效果
        this.applySpecialFlightEffects(node);
    }

    private applySpecialFlightEffects(node: EvolutionNode) {
        // 处理特殊飞行能力
        const effects = node.effects;
        
        if (effects.stormResistance) {
            this.enableStormFlight(effects.stormResistance);
        }
        if (effects.thermalSoaring) {
            this.enableThermalSoaring(effects.thermalSoaring);
        }
        if (effects.windControl) {
            this.enhanceWindControl(effects.windControl);
        }
    }

    private enableStormFlight(level: number) {
        const stormResistance = this.getComponent('StormResistance');
        if (stormResistance) {
            stormResistance.setLevel(level);
        }
    }

    private enableThermalSoaring(level: number) {
        const thermalSoaring = this.getComponent('ThermalSoaring');
        if (thermalSoaring) {
            thermalSoaring.setLevel(level);
        }
    }

    private enhanceWindControl(level: number) {
        const windControl = this.getComponent('WindControl');
        if (windControl) {
            windControl.setLevel(level);
        }
    }

    public canEvolve(targetStage: string): boolean {
        const node = this.evolutionNodes.get(targetStage);
        if (!node) return false;

        return node.requirements.every(req => {
            const reqNode = this.evolutionNodes.get(req);
            return reqNode?.isUnlocked;
        });
    }
} 